package com.zhupf.part3;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhupf
 * @date 2023年10月26日 17:17
 * @Description
 */
public class LengthOfLongestSubstring {

    public static void main(String[] args) {

        String s = "abcabcbb";
        System.out.println(lengthOfLongestSubstring(s));

    }

    public static int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int idxL = 0;



        int idxR = 0;
        int length = 0;
        Set<Character> set = new HashSet<>();
        int curLenth = 0;
        while (idxR < chars.length){
            if(!set.contains(chars[idxR])){
                set.add(chars[idxR++]);
                curLenth++;
                continue;
            }
            length = Math.max(length,curLenth);
            while (idxL<idxR){
                set.remove(chars[idxL]);
                curLenth --;
                if(chars[idxL++] == chars[idxR]){
                   break;
                }
            }
        }
        return Math.max(length,curLenth);
    }
}
